<template>
    <el-card>
        <el-form :model="queryParams" ref="queryParams" :inline="true" label-width="100px">
            <el-form-item label="插件名称" prop="engName">
                <el-input v-model="queryParams.engName" placeholder="插件名称" size="small"></el-input>
            </el-form-item>
            <el-form-item label="插件描述" prop="chnName">
                <el-input v-model="queryParams.chnName" placeholder="插件描述" size="small"></el-input>
            </el-form-item>
            <el-form-item >
                <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-delete" size="small" @click="resetQuery('queryParams')">清空</el-button>
            </el-form-item>
        </el-form>
         <div style="margin-bottom: 10px">
            <el-button type="primary" size="small" icon="el-icon-plus" @click="handleAdd">新增</el-button>
        </div>
        <el-table :data="tableData" border style="width: 100%" size="small">
            <el-table-column type="index" label="#" width="50"></el-table-column>
            <el-table-column label="ID" prop="id" width="50"></el-table-column>
            <el-table-column label="插件名称" prop="engName"></el-table-column>
            <el-table-column label="插件描述" prop="chnName"></el-table-column>
            <el-table-column label="表单定义" prop="config"></el-table-column>
            <el-table-column label="自定义配置" prop="fixedConfig"></el-table-column>
            <el-table-column label="操作" width="200" align="center">
                <template slot-scope="scope">
                        <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">查看</el-button>
                        <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
                        <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                v-show="total>0"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                background
                :current-page="1"
                :page-sizes="[10, 20, 30, 40,50,100]"
                :page-size="10"
                layout="total, sizes, prev, pager, next, jumper"
                class="pagination-container"
                :total="total">
         </el-pagination>
        <!-- 弹窗 -->
        <el-dialog
            :title="formTitle"
            :visible.sync="addDialogVisible"
            width="50%"
            >
            <el-form :model="form" ref="form" label-width="120px" :disabled="mode==='view'">
              <el-form-item label="插件名称" required prop="engName">
                  <el-input v-model="form.engName" placeholder="请输入插件名称" size="small"></el-input>
                  <div style="font-size: 14px">插件名称由英文字母、下划线或数字组成, 不能以数字开头，如：memberAuth, member_auth_v1</div>
              </el-form-item>
               <el-form-item label="插件分类" required>
                  <el-row>
                    <el-col :span="10">
                      <el-input v-model="form.name" placeholder="请输入插件名称" size="small"></el-input>
                    </el-col>
                  </el-row>
              </el-form-item>
               <el-form-item label="插件描述" required prop="chnName">
                  <el-row>
                    <el-col :span="21">
                      <el-input v-model="form.chnName" placeholder="请输入插件描述" size="small"></el-input>
                      如：会员登录校验
                    </el-col>
                  </el-row>
              </el-form-item>
               <el-form-item label="默认执行顺序" required prop="order">
                  <el-row>
                    <el-col :span="20">
                      <el-input v-model="form.order" placeholder="请输入插件名称" size="small"></el-input>
                    </el-col>
                  </el-row>
              </el-form-item>
               <el-form-item label="表单定义">
                  <el-row>
                    <el-col :span="21">
                    </el-col>
                  </el-row>
              </el-form-item>
              <el-form-item label="自定义配置" prop="config">
                  <el-row>
                    <el-col :span="20">
                      <el-input
                        type="textarea"
                        :rows="2"
                        placeholder="请输入内容"
                        v-model="form.config">
                      </el-input>
                      插件级别的自定义配置信息 (插件实例)
                    </el-col>
                  </el-row>
              </el-form-item>
               <el-form-item label="使用说明" prop="instruction">
                  <el-row>
                    <el-col :span="20">
                      <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="form.instruction">
                      </el-input>
                    </el-col>
                  </el-row>
              </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer" v-if="mode !== 'view'">
                <el-button @click="dialogVisible = false" size="mini">取 消</el-button>
                <el-button type="primary" @click="dialogVisible = false" size="mini">确 定</el-button>
            </span>
        </el-dialog>
    </el-card>
</template>

<script>
import { getPluginList } from '@/api/getway/plugin.api'
    export default {
        data() {
            return {
                queryParams: {
                  current:1,
                  size: 10,
                  engName: undefined,
                  chnName: undefined
                },
                total: 0,
                tableData: [],
                addDialogVisible: false,
                form: {},
                formTitle: '新增',
                mode: 'add'
            }
        },
        mounted() {
          this.getList();
        },
        methods: {
            handleAdd() {
                this.addDialogVisible = true;
            },
            handleView(row) {
              this.form = JSON.parse(JSON.stringify(row));
              this.formTitle = '查看';
              this.addDialogVisible = true;
              this.mode = 'view';
            },
            handleEdit(row) {
               this.form = JSON.parse(JSON.stringify(row));
               this.formTitle = '编辑';
               this.addDialogVisible = true;
               this.mode = 'edit';
            },
            // 搜索
            handleQuery() {
                this.queryParams.current = 1;
                this.getList();
            },
            // 清空
            resetQuery(formName) {
                this.$refs[formName].resetFields();
                this.getList();
            },
            async getList() {
              const result = await getPluginList(this.queryParams);
              if(result.code === 200) {
                this.tableData = result.data.records;
                this.total = result.data.total;
              }
            },
            handleSizeChange(val) {
                this.queryParams.size = val;
                this.getList();
            },
            handleCurrentChange(val) {
                this.queryParams.current = val;
                this.getList();
            }
        }
    }
</script>

<style lang="scss" scoped>
.pagination-container {
    padding: 10px;
    text-align: right;
}
</style>